//
//  WXLiveTextView.m
//  TestTextView
//
//  Created by tianlong on 2019/3/27.
//  Copyright © 2019年 tianlong. All rights reserved.
//

#import "WXLiveTextView.h"
#import "Masonry.h"

@interface WXLiveTextView ()<UITextViewDelegate>
@property (weak,   nonatomic) UITextView *placeView;
@property (weak,   nonatomic) UITextView *inputView;
@property (weak,   nonatomic) UILabel *tipLabel;
@property (copy,   nonatomic) NSString *tip;
@property (assign, nonatomic) BOOL firstInit;
@property (assign, nonatomic) CGFloat initHeight;
@end

@implementation WXLiveTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //提示
        UILabel *tipLabel = [[UILabel alloc] init];
        [self addSubview:tipLabel];
        self.tipLabel = tipLabel;
        
        //输入
        UITextView *inputView = [[UITextView alloc] init];
        inputView.delegate = self;
        inputView.scrollEnabled = NO;
        [self addSubview:inputView];
        self.inputView = inputView;
        
        //占位
        UITextView *placeView = [[UITextView alloc] init];
        placeView.backgroundColor = [UIColor clearColor];
        placeView.textColor = [UIColor grayColor];
        placeView.userInteractionEnabled = NO;
        [self addSubview:placeView];
        self.placeView = placeView;

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(textFieldEditChanged:)
                                                    name:UITextViewTextDidChangeNotification object:inputView];
        
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.firstInit = YES;
    }
    return self;
}

#pragma mark - 通知回调
-(void)textFieldEditChanged:(NSNotification *)note {
    _text = self.inputView.text;
    self.placeView.hidden = self.inputView.text.length > 0;
    
    CGRect frame = self.frame;
    
    if (self.firstInit) {
        self.initHeight = frame.size.height;
        self.firstInit = NO;
    }
    
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [self.inputView sizeThatFits:constraintSize];
    CGFloat viewH = MAX(self.firstInit, size.height);
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(frame.origin.x);
        make.top.offset(frame.origin.y);
        make.width.offset(frame.size.width);
        make.height.offset(viewH);
    }];
    
    NSLog(@"size ==== %@",NSStringFromCGSize(size));
    [self updateLayout];
    NSLog(@"inputView.text = %@",_text);
    
    if([self.delegate respondsToSelector:@selector(live_textView:currentText:viewHeight:)]) {
        [self.delegate live_textView:self currentText:self.text viewHeight:viewH];
    }
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if ([self.delegate respondsToSelector:@selector(live_textViewShouldBeginEditing:)]) {
        return [self.delegate live_textViewShouldBeginEditing:self];
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([self.delegate respondsToSelector:@selector(live_textView:shouldChangeTextInRange:replacementText:)]) {
        return [self.delegate live_textView:self shouldChangeTextInRange:range replacementText:text];
    }
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([self.delegate respondsToSelector:@selector(live_textViewDidBeginEditing:)]) {
        [self.delegate live_textViewDidBeginEditing:self];
    }
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    if ([self.delegate respondsToSelector:@selector(live_textViewShouldEndEditing:)]) {
        return [self.delegate live_textViewShouldEndEditing:self];
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if ([self.delegate respondsToSelector:@selector(live_textViewDidEndEditing:)]) {
        [self.delegate live_textViewDidEndEditing:self];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self endEditing:YES];
}


#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    [self updateLayout];
}

- (void)updateLayout {
    __weak typeof(self)selfWeak = self;
    if (self.tipLabel.text.length > 0) {
        [self.tipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(0);
            make.bottom.offset(0);
        }];
        
        [self.inputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.offset(0);
            make.bottom.equalTo(selfWeak.tipLabel.mas_top);
        }];
        [self.placeView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(selfWeak.inputView.mas_left);
            make.top.equalTo(selfWeak.inputView.mas_top);
            make.bottom.equalTo(selfWeak.inputView.mas_bottom);
            make.right.equalTo(selfWeak.inputView.mas_right);
        }];
    } else {
        [self.inputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.offset(0);
        }];
        [self.placeView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(selfWeak.inputView.mas_left);
            make.top.equalTo(selfWeak.inputView.mas_top);
            make.bottom.equalTo(selfWeak.inputView.mas_bottom);
            make.right.equalTo(selfWeak.inputView.mas_right);
        }];
    }
}

#pragma mark - setter
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    if (placeholder) {
        self.placeView.text = placeholder;
    }
}

- (void)setTextFont:(UIFont *)textFont {
    _textFont = textFont;
    if (textFont) {
        self.inputView.font = textFont;
        self.placeView.font = textFont;
    }
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    if (textColor) {
        self.inputView.textColor = textColor;
    }
}

- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor {
    _placeholderTextColor = placeholderTextColor;
    if (placeholderTextColor) {
        self.placeView.textColor = placeholderTextColor;
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"%s",__FUNCTION__);
}

@end
